package com.architecture.web.jsf.util;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 * <p>This class has utilities methods for be used with (Java Server Faces Framework - JSF).</p>
 * @author JR
 * @version 1.0
 * @category Java Server Faces Utilities
 */
public final class FacesUtility{
	
	/**
	 * private constructor of <code>FacesUtility</code>.
	 */
	private FacesUtility() {
		super();
	}

	/**
	 * <p>Gets the current instance of {@linkplain FacesContext}.</p>
	 * @return <code>FacesContext</code>
	 */
	public static FacesContext getCurrentInstance() {
		return FacesContext.getCurrentInstance();
	}

	/**
	 * <p>Gets the {@linkplain ExternalContext}</p>
	 * @return <code>ExternalContext</code>
	 */
	public static ExternalContext getExternalContext() {
		return getCurrentInstance().getExternalContext();
	}

	/**
	 * <p>Add warning message.</p>
	 * @param message
	 */
	public static void addWarningMessage(String message) {
		getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, message, null));
	}

	/**
	 * <p>Add error message.</p>
	 * @param message
	 */
	public static void addErrorMessage(String message) {
		getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
	}

	/**
	 * <p>Add success message.</p>
	 * @param message
	 */
	public static void addSuccessMessage(String message) {
		getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
	}
	
	/**
	 * <p>Verify if has messages into the JSF context.</p>
	 * @return <code>Boolean<code>
	 */
	public static Boolean hasMessages() {
		return getCurrentInstance().getMessages().hasNext();
	}
	
}
